import { call, put, fork, select, take } from 'redux-saga/effects'
import { message } from 'antd'
import actions,{ GET_DATA } from '@/actions/sendRecord'
import api from '@/api/sendRecord'
import util from '@/utils'
import moment from 'moment'

function* getData() {
  try {
    const params = yield select(({ sendRecordReducer }) => util.pick(sendRecordReducer,[
      'batchId',
      'endTime',
      'messageId',
      'operator',
      'pageNum',
      'pageSize',
      'phone',
      'startTime',
      'status'
    ])) 
    const { startTime, endTime } = params       
    params.startTime = util.formatDay(startTime)
    params.endTime = util.formatDay(endTime)
    const { isSuccess, data, desc } = yield call(api.getSendRecordData, params)
    if(isSuccess && data && Array.isArray(data.list)) {
      yield put(actions.getDataSuccess(data))
      //!data.size && message.warning(desc) 
    }else {
      throw new Error(desc)
    }
  } catch (error) {
    yield put(actions.getDataFail())
    message.error(error.message || '获取数据错误')
  }
}

function* watchGetData() {
  while (true) {
    yield take(GET_DATA)
    yield call(getData)
  }
}

export default [
  fork(watchGetData),
]